package cn.wolfcode.wms.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.wolfcode.wms.domain.Employee;
import cn.wolfcode.wms.domain.SystemMenu;
import cn.wolfcode.wms.mapper.SystemMenuMapper;
import cn.wolfcode.wms.query.PageResult;
import cn.wolfcode.wms.query.QueryObject;
import cn.wolfcode.wms.service.ISystemMenuService;
import cn.wolfcode.wms.util.UserContext;

@Service
public class SystemMenuServiceImpl implements ISystemMenuService {

	@Autowired
	private SystemMenuMapper mapper;
	
	@Override
	public void save(SystemMenu systemmenu) {
		mapper.save(systemmenu);
	}

	@Override
	public void delete(Long id) {
		mapper.delete(id);
	}

	@Override
	public void update(SystemMenu systemmenu) {
		mapper.update(systemmenu);
	}

	@Override
	public SystemMenu get(Long id) {
		return mapper.get(id);
	}

	@Override
	public List<SystemMenu> list() {
		return mapper.list();
	}

	@Override
	public PageResult query(QueryObject qo) {
		int totalCount = mapper.queryForCount(qo);
		if (totalCount == 0) {
			return new PageResult(qo.getPageSize());
		}
		List<SystemMenu> listData= mapper.queryForList(qo);
		return new PageResult(qo.getCurrentPage(),qo.getPageSize(), listData, totalCount);
	}

	@Override
	public List<Map<String, Object>> selectByParentSn(String parentSn) {
		Employee currentUser = UserContext.getUser();
		if (currentUser.isAdmin()) {
			return mapper.selectByParentSn(parentSn);
		}else {
			return mapper.selectByParentSnAndEmpId(parentSn,currentUser.getId());
		}
	}

}
